package com.aaa.service.impl;

import com.aaa.dao.LoginInfoDao;
import com.aaa.entity.LoginInfo;
import com.aaa.entity.OperLog;
import com.aaa.service.LoginInfoService;
import com.aaa.vo.LoginInfoVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsPackage: com.aaa.service.impl
 * @Description:
 * @CreateTime: 2023-03-31  09:15
 * @Author: ql
 */
@Service
public class LoginInfoServiceImpl implements LoginInfoService {

    @Autowired
    private LoginInfoDao loginInfoDao;
    @Override
    public Result<IPage<LoginInfo>> queryByPage(LoginInfoVo loginInfoVo) {
        Page<LoginInfo> loginInfoPage = new Page<>(loginInfoVo.getPageNum(),loginInfoVo.getPageSize());
        QueryWrapper<LoginInfo> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(loginInfoVo.getUserName())){
            wrapper.like("user_name",loginInfoVo.getUserName());
        }if (StringUtils.hasText(loginInfoVo.getLoginAccount())){
            wrapper.like("login_account",loginInfoVo.getLoginAccount());
        }if (StringUtils.hasText(loginInfoVo.getIpAddr())){
            wrapper.like("ip_addr",loginInfoVo.getIpAddr());
        }if (StringUtils.hasText(loginInfoVo.getLoginStatus())){
            wrapper.eq("login_status",loginInfoVo.getLoginStatus());
        }if (StringUtils.hasText(loginInfoVo.getLoginType())){
            wrapper.like("login_type",loginInfoVo.getLoginType());
        }
        if (loginInfoVo.getBeginTime()!=null&&loginInfoVo.getEndTime()!=null){
            wrapper.between("login_time",loginInfoVo.getBeginTime(),loginInfoVo.getEndTime());
        }
        IPage<LoginInfo> loginInfoPage1 = loginInfoDao.selectPage(loginInfoPage, wrapper);
        return new Result<>(2000,"ok",loginInfoPage1);
    }

    @Override
    public Result<String> deleteLogByIds(Long[] id) {
        List<Long> collect1 = Arrays.stream(id).collect(Collectors.toList());
        System.out.println("===========多选删除id==============="+collect1);
        int i = loginInfoDao.deleteBatchIds(collect1);
        if (i>0){
            return Result.success();
        }
        return Result.error();
    }

    @Override
    public Result<String> delAll() {
        QueryWrapper<LoginInfo> wrapper = new QueryWrapper<>();
        int delete = loginInfoDao.delete(wrapper);
        if (delete>0){
            return Result.success();
        }
        return Result.error();
    }
}
